/**
 * 有N张牌，从下到上分别是[1,2,3,...,N]
 * 有M种技能，每一种技能代价为ai，收益是可以将上bi张牌放到下面，相当于
 * [1,2,3,...,N-bi+1,...,N] => [N-bi+1,...,N,1,2,3,...,N-bi]
 * 问要将初始的第K张牌移动到顶，最小代价
 * 建图，一共有N个顶点，第i个定点到`(i+ai) if i+ai<=N else i+ai-N`有一条权值为bi的边
 * 从K开始跑一个最短路，答案是D[N]
 */
#include <bits/stdc++.h>
using namespace std;

#include <bits/extc++.h>

using llt = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using weight_t = pair<int, llt>;

int N, M, K;
vector<vector<weight_t>> G;

__gnu_pbds::priority_queue<weight_t, function<bool(const weight_t &, const weight_t &)>> Q([](const weight_t & a, const weight_t & b){
    if(a.second != b.second) return a.second > b.second;
    return a.first > b.first;
});

vector<llt> D;
vi Flag;

llt proc(){
    D.assign(N + 1, 0x7F8F9FAFBFCFDFEF);

    Q.clear();
    Q.push({K, D[K] = 0});    
    Flag.assign(N + 1, 0);

    while(1){
        while(not Q.empty() and Flag[Q.top().first]) Q.pop();
        if(Q.empty()) break;

        auto h = Q.top(); Q.pop();
        Flag[h.first] = 1;

        int v;
        llt tmp;
        for(const auto & p : G[h.first]){
            if(Flag[v = p.first]) continue;
            if(D[v] <= (tmp = h.second + p.second)) continue;

            Q.push({v, D[v] = tmp}); 
        }
    }
    
    return D[N] == 0x7F8F9FAFBFCFDFEF ? -1 : D[N];
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int nofkase = 1; 
    cin >> nofkase;
    while(nofkase--){
        cin >> N >> M >> K;
        G.assign(N + 1, {});
        for(int a,b,i=1;i<=M;++i){
            cin >> a >> b;
            for(int j=1;j<=N;++j){
                if(j + a <= N){
                    G[j].emplace_back(j + a, b);
                }else{
                    G[j].emplace_back(j + a - N, b);
                }
            }
        }
        cout << proc() << "\n";
    }
    return 0;
}